﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using coffeeshop.Models;

namespace coffeeshop.Forms
{
    public partial class frmEditUnit : Form
    {
        private Unit theUnit { get; set; }
        public Form MyParentForm { get; set; }

        public frmEditUnit(Unit _unit)
        {
            InitializeComponent();
            if (_unit != null)
            {
                theUnit = _unit;
                LoadUnitInfo();
            }
        }

        private void LoadUnitInfo()
        {
            if (theUnit != null)
            {
                txtName.Text = theUnit.Name;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // verify the user input
            bool hasEmptyTextBox = Controls.OfType<TextBox>()
                               .Any(tb => String.IsNullOrWhiteSpace(tb.Text));

            if (hasEmptyTextBox)
            {
                MessageBox.Show("Bạn cần điển đầy đủ thông tin");
                return;
            }

            if (theUnit == null)
            {
                theUnit = new Unit();
            }

            theUnit.Name = txtName.Text.Trim();
            if (theUnit.Save())
            {
                // Reload the list
                frmUnitList userList = (frmUnitList)this.MyParentForm;
                userList.ReloadList();
                this.Close();
            }
            else
            {
                MessageBox.Show("Có lỗi trong quá trình lưu");
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
